package com.example.isoftservice.controller;

import com.example.isoftservice.pojos.Category;
import com.example.isoftservice.pojos.InstitutionBigCategory;
import com.example.isoftservice.service.CategoryService;
import com.example.isoftservice.service.InstitutionBigCategoryService;
import com.example.isoftservice.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/bigCategory")
public class InstitutionBigCategoryController {

    @Resource
    private InstitutionBigCategoryService institutionBigCategoryService;


    @RequestMapping(value = "/searchBigCategoryList",method = RequestMethod.GET)
    public Result<Map<String,Object>> searchBigCategoryList(@RequestParam(name = "pageNo",defaultValue = "1") Integer pageNo,
                                                         @RequestParam(name = "pageSize",defaultValue = "5") Integer pageSize) {
        Result<Map<String, Object>> result = new Result<>();
        result.setCode(0);
        result.setResult(institutionBigCategoryService.searchBigCategoryList(pageNo, pageSize));
        result.setMessage("查询成功");
        result.setSuccess(true);
        return result;
    }

    @RequestMapping(value = "/searchBigCategoryById",method = RequestMethod.GET)
    public Result<InstitutionBigCategory> searchBigCategoryById(Integer institutionBigCategoryId) {
        Result<InstitutionBigCategory> result = new Result<>();
        InstitutionBigCategory institutionBigCategory = institutionBigCategoryService.searchBigCategoryById(institutionBigCategoryId);
        if(institutionBigCategory != null) {
            result.setCode(0);
            result.setResult(institutionBigCategory);
            result.setMessage("查询成功");
            result.setSuccess(true);
            return result;
        } else {
            result.setCode(1);
            result.setMessage("数据不存在");
            return result;
        }

    }

    //按institutionBigCategoryId查询大类名称和所属小类名称
    @RequestMapping(value = "/searchBelong",method = RequestMethod.GET)
    public Result<Map<String,Object>> searchBigCategoryList(Integer institutionBigCategoryId,
                                                            @RequestParam(name = "pageNo",defaultValue = "1") Integer pageNo,
                                                            @RequestParam(name = "pageSize",defaultValue = "5") Integer pageSize) {
        Result<Map<String, Object>> result = new Result<>();
        result.setCode(0);
        result.setResult(institutionBigCategoryService.searchBelong(institutionBigCategoryId,pageNo, pageSize));
        result.setMessage("查询成功");
        result.setSuccess(true);
        return result;
    }


    @RequestMapping(value = "/addBigCategory",method = RequestMethod.POST)
    public Result<Boolean> addBigCategory(@RequestBody InstitutionBigCategory institutionBigCategory) {
        Result<Boolean> result = new Result<>();
        result.setCode(0);
        boolean flag = institutionBigCategoryService.addBigCategory(institutionBigCategory);
        result.setResult(flag);
        result.setMessage("增加成功");
        result.setSuccess(flag);
        return result;
    }

    @RequestMapping(value = "/removeBigCategoryByIds",method = RequestMethod.POST)
    public Result<Boolean> removeCategoryByIds(@RequestBody InstitutionBigCategory institutionBigCategory) {
        Result<Boolean> result = new Result<>();
        result.setCode(0);
        boolean flag = institutionBigCategoryService.removeBigCategoryByIds(institutionBigCategory.ids);
        result.setResult(flag);
        result.setMessage("删除成功");
        result.setSuccess(flag);
        return result;
    }


    @RequestMapping(value = "/modifyBigCategory",method = RequestMethod.POST)
    public Result<Boolean> modifyBigCategory(@RequestBody InstitutionBigCategory institutionBigCategory) {
        Result<Boolean> result = new Result<>();
        if(institutionBigCategoryService.searchBigCategoryById(institutionBigCategory.getInstitutionBigCategoryId()) == null ) {
            result.setCode(1);
            result.setMessage("该数据不存在");
            return result;
        }
        result.setCode(0);
        boolean flag = institutionBigCategoryService.modifyBigCategoryById(institutionBigCategory);
        result.setResult(flag);
        result.setMessage("更新成功");
        result.setSuccess(flag);
        return result;
    }
}
